import datetime
from sqlalchemy import Column, String, Integer, Float, DateTime, create_engine
from sqlalchemy.ext.declarative import declarative_base
import holiday

# 创建对象的基类:
Base = declarative_base()

# 定义User对象:
class DaoIssue(Base):
    # 表的名字:
    __tablename__ = 'issue'
    issue_key = Column(String(64), primary_key=True)
    project = Column(String(32))
    issuetype = Column(String(64))
    sprint = Column(String(64))
    summary = Column(String(512))
    status = Column(String(32))
    istatus = Column(Integer, default=0)
    created_at = Column(DateTime)
    updated_at = Column(DateTime)
    fix_versions = Column(String(64))
    team = Column(String(32))  # customfield_10500
    estimated = Column(Integer)
    creator = Column(String(64))
    developer = Column(String(64))
    qa = Column(String(64))
    ba = Column(String(64))
    time_dev_start = Column(DateTime)
    time_dev_end = Column(DateTime)
    time_dev_used = Column(Float, default=0)
    time_dev_count = Column(Integer, default=0)
    time_qa_start = Column(DateTime)
    time_qa_end = Column(DateTime)
    time_qa_used = Column(Integer, default=0)
    time_ba_start = Column(DateTime)
    time_ba_end = Column(DateTime)
    time_ba_used = Column(Integer, default=0)
    time_uat_used = Column(Integer, default=0)
    end_at = Column(DateTime)
    start_at = Column(DateTime)
    used = Column(Float, default=0)
    changelogs = Column(String(512))

    def __str__(self):
        return "{}({}):start_at={},end_at={},use={},time_dev_used={},time_qa_used={},time_ba_used={},time_uat_used={}".format(self.issue_key,self.sprint, self.start_at, self.end_at, self.used, self.time_dev_used, self.time_qa_used, self.time_ba_used, self.time_uat_used)

    def caculateUsedDays(self):
        if self.end_at is None or self.start_at is None:
            return
        self.used = holiday.getWorkDays(self.start_at, self.end_at)


class Release(Base):
    __tablename__ = 'releases'
    id = Column(Integer, primary_key=True)
    project = Column(String(32))
    name = Column(String(32))
    release_date = Column(DateTime)


class DaoProject(Base):
    __tablename__ = 'projects'
    id = Column(Integer, primary_key=True)
    site = Column(String(32)) 
    oid = Column(String(32))
    name = Column(String(32))
    project_key = Column(String(32))
    statuses = Column(String(1024))
    versions = Column(String(1024))
    updated_at = Column(DateTime)
    issues = Column(Integer)

    def to_s(self):
        return "key={},name={}".format(self.name, self.project_key)

class DaoChangeLog(Base):
    __tablename__ = 'change_logs'
    id = Column(Integer, primary_key=True)
    issue_id = Column(String(32))
    oid = Column(String(32))
    project_key = Column(String(512))
    author = Column(String(512))
    items = Column(String(512))
    updated_at = Column(DateTime)

class History(Base):
    __tablename__ = 'histories'
    id = Column(Integer, primary_key=True)
    issue_key = Column(String(64))
    author = Column(String(64))
    action = Column(String(64))
    status_in = Column(DateTime)
    status_out = Column(DateTime)
    duration = Column(Integer)
    project = Column(String(64))
